package ua.kpi.oblamskyiv;

import ua.kpi.oblamskyiv.factory.QuestionsFactory;
import ua.kpi.oblamskyiv.io.TesterIO;
import ua.kpi.oblamskyiv.testcase.Answers;
import ua.kpi.oblamskyiv.testcase.Questions;
import ua.kpi.oblamskyiv.testcase.question.Question;

public class Tester {
    private Questions questions;
    private QuestionsFactory questionsFactory;
    private TesterIO testerIO;

    public Tester(QuestionsFactory questionsFactory) {
        this(questionsFactory, new TesterIO(System.in, System.out));
    }

    public Tester(QuestionsFactory questionsFactory, TesterIO testerIO) {
        this.testerIO = testerIO;
        this.questionsFactory = questionsFactory;

        questions = this.questionsFactory.createQuestions();
    }

    public TestStats takeTest(User user) {
        TestStats testStats = new TestStats(user);
        questions.shuffle();

        for (Question question : questions) {
            testerIO.outQuestion(question);
            Answers userAnswers = testerIO.getUserAnswers(question);
            testStats.updateStats(question, userAnswers);
        }
        testerIO.outResult(testStats.getScore());

        return testStats;
    }
}
